﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TDME
{   
    public partial class Main : Form
    {
        private byte[] map;
        int width = 0;
        int height = 0;

        public Main()
        {
            InitializeComponent();
            
            

        }       

        private void pnlControls_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = pnlControls.CreateGraphics();

            g.DrawLine(Pens.LightGray, new Point(lblBit_1.Location.X - 4, lblBit_1.Location.Y), new Point(lblBit_1.Location.X - 4, chkBit_1.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_2.Location.X - 4, lblBit_2.Location.Y), new Point(lblBit_2.Location.X - 4, chkBit_2.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_3.Location.X - 4, lblBit_3.Location.Y), new Point(lblBit_3.Location.X - 4, chkBit_3.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_4.Location.X - 4, lblBit_4.Location.Y), new Point(lblBit_4.Location.X - 4, chkBit_4.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_5.Location.X - 4, lblBit_5.Location.Y), new Point(lblBit_5.Location.X - 4, chkBit_5.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_6.Location.X - 4, lblBit_6.Location.Y), new Point(lblBit_6.Location.X - 4, chkBit_6.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_7.Location.X - 4, lblBit_7.Location.Y), new Point(lblBit_7.Location.X - 4, chkBit_7.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_8.Location.X - 4, lblBit_8.Location.Y), new Point(lblBit_8.Location.X - 4, chkBit_8.Location.Y + 13));

            g.DrawLine(Pens.LightGray, new Point(lblBit_8.Location.X - 4, lblBit_8.Location.Y), new Point(lblBit_8.Location.X - 4, chkBit_8.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_9.Location.X - 4, lblBit_9.Location.Y), new Point(lblBit_9.Location.X - 4, chkBit_9.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_10.Location.X - 4, lblBit_10.Location.Y), new Point(lblBit_10.Location.X - 4, chkBit_10.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_11.Location.X - 4, lblBit_11.Location.Y), new Point(lblBit_11.Location.X - 4, chkBit_11.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_12.Location.X - 4, lblBit_12.Location.Y), new Point(lblBit_12.Location.X - 4, chkBit_12.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_13.Location.X - 4, lblBit_13.Location.Y), new Point(lblBit_13.Location.X - 4, chkBit_13.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_14.Location.X - 4, lblBit_14.Location.Y), new Point(lblBit_14.Location.X - 4, chkBit_14.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_15.Location.X - 4, lblBit_15.Location.Y), new Point(lblBit_15.Location.X - 4, chkBit_15.Location.Y + 13));
            g.DrawLine(Pens.LightGray, new Point(lblBit_16.Location.X - 4, lblBit_16.Location.Y), new Point(lblBit_16.Location.X - 4, chkBit_16.Location.Y + 13));

            setLblByte1();
            setLblByte2();
        }   

        private void chkBit_1_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_1.Checked)
            {
                lblBit_1.Text = "1";
            }
            else
            {
                lblBit_1.Text = "0";
            }
            setLblByte1();

        }

        private void chkBit_2_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_2.Checked)
            {
                lblBit_2.Text = "1";
            }
            else
            {
                lblBit_2.Text = "0";
            }
            setLblByte1();
        }

        private void chkBit_3_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_3.Checked)
            {
                lblBit_3.Text = "1";
            }
            else
            {
                lblBit_3.Text = "0";
            }
            setLblByte1();
        }

        private void chkBit_4_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_4.Checked)
            {
                lblBit_4.Text = "1";
            }
            else
            {
                lblBit_4.Text = "0";
            }
            setLblByte1();
        }

        private void chkBit_5_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_5.Checked)
            {
                lblBit_5.Text = "1";
            }
            else
            {
                lblBit_5.Text = "0";
            }
            setLblByte1();
        }

        private void chkBit_6_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_6.Checked)
            {
                lblBit_6.Text = "1";
            }
            else
            {
                lblBit_6.Text = "0";
            }
            setLblByte1();
        }

        private void chkBit_7_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_7.Checked)
            {
                lblBit_7.Text = "1";
            }
            else
            {
                lblBit_7.Text = "0";
            }
            setLblByte1();
        }

        private void chkBit_8_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_8.Checked)
            {
                lblBit_8.Text = "1";
            }
            else
            {
                lblBit_8.Text = "0";
            }
            setLblByte1();
        }   

        private void chkBit_9_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_9.Checked)
            {
                lblBit_9.Text = "1";
            }
            else
            {
                lblBit_9.Text = "0";
            }
            setLblByte2();
        }

        private void chkBit_10_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_10.Checked)
            {
                lblBit_10.Text = "1";
            }
            else
            {
                lblBit_10.Text = "0";
            }
            setLblByte2();
        }

        private void chkBit_11_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_11.Checked)
            {
                lblBit_11.Text = "1";
            }
            else
            {
                lblBit_11.Text = "0";
            }
            setLblByte2();
        }

        private void chkBit_12_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_12.Checked)
            {
                lblBit_12.Text = "1";
            }
            else
            {
                lblBit_12.Text = "0";
            }
            setLblByte2();
        }

        private void chkBit_13_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_13.Checked)
            {
                lblBit_13.Text = "1";
            }
            else
            {
                lblBit_13.Text = "0";
            }
            setLblByte2();
        }

        private void chkBit_14_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_14.Checked)
            {
                lblBit_14.Text = "1";
            }
            else
            {
                lblBit_14.Text = "0";
            }
            setLblByte2();
        }

        private void chkBit_15_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_15.Checked)
            {
                lblBit_15.Text = "1";
            }
            else
            {
                lblBit_15.Text = "0";
            }
            setLblByte2();
        }

        private void chkBit_16_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBit_16.Checked)
            {
                lblBit_16.Text = "1";
            }
            else
            {
                lblBit_16.Text = "0";
            }
            setLblByte2();
        }

        private void setLblByte1()
        {
            String buffer = lblBit_1.Text
                + lblBit_2.Text
                + lblBit_3.Text
                + lblBit_4.Text
                + lblBit_5.Text
                + lblBit_6.Text
                + lblBit_7.Text
                + lblBit_8.Text;

            int wert = Convert.ToInt32(buffer, 2);

            lblByte_1.Text = " => 0x" + wert.ToString("X2");

        }

        private void setLblByte2()
        {
            String buffer = lblBit_9.Text
                + lblBit_10.Text
                + lblBit_11.Text
                + lblBit_12.Text
                + lblBit_13.Text
                + lblBit_14.Text
                + lblBit_15.Text
                + lblBit_16.Text;

            int wert = Convert.ToInt32(buffer, 2);

            lblByte_2.Text = "=> 0x" + wert.ToString("X2");

        }

        //*///////////////////////////////////////////////////////////////

        private void neuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewMap nm = new NewMap(this);
            DialogResult res = nm.ShowDialog();
        }


        /// <summary>
        /// Ladet eine Map Datei aus dem Ordner "maps" ins Programm.
        /// </summary>
        /// <param name="Mapname"></param>
        public void LoadMapFromFile(String Mapname)
        {
            FileStream mapFile = null;
            try
            {
                 mapFile = new FileStream("maps\\" + Mapname, FileMode.Open, FileAccess.ReadWrite);
            }
            catch 
            {
                return;
            }

            map = new Byte[mapFile.Length];

            mapFile.Read(map, 0, map.Length);

            Byte[] buffer = {0, 0, map[2], map[3]};
            this.width = BitConverter.ToInt32(buffer, 0);

            buffer[2] = map[4];
            buffer[3] = map[5];
            this.height = BitConverter.ToInt32(buffer, 0);
        }


        /// <summary>
        /// Generiert eine Datei -Mapname-.map mit den Abmessungen Width und Height.
        /// 
        /// | 1. Byte Format Version 
        /// | 2-3. Byte Map Breite
        /// | 4-5. Byte Map Höhe
        /// | 6-9. Byte Map erstellt
        /// | 10-13. Byte Map editiert
        /// | 14-15. Byte Länge des Mapnamens
        /// | 16-17 Byte Länge des Ersteller
        /// | Mapname
        /// | Ersteller
        /// | Felder (jeweils zwei Byte) 
        /// </summary>
        /// <param name="Mapname"></param>
        /// <param name="Editor"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        /// <returns>Name der Datei</returns>
        public static string GenerateMap(String Mapname, String Editor, ushort Width, ushort Height)
        {
            FileStream map = new FileStream("maps\\" + Mapname + ".map", FileMode.Create, FileAccess.ReadWrite);

            int offset = 0;
            
            DateTime date_ref = new DateTime(1970, 1, 1);  //Refernzdatum (festgelegt)
            DateTime date_now = DateTime.Now;              //jetztiges Datum / Uhrzeit
            TimeSpan ts = new TimeSpan(date_now.Ticks - date_ref.Ticks);  // das Delta ermitteln
            // Das Delta als gesammtzahl der sekunden ist der Timestamp
            Byte[] erstellt = Main.GetByteArray(Convert.ToInt32(ts.TotalSeconds));
            
            Byte[] daten = new Byte[96 + (Width * Height * 2)];

            daten[0] = 0x01; // Version
            daten[1] = Main.GetByteArray(Width)[1];
            daten[2] = Main.GetByteArray(Width)[0];
            daten[3] = Main.GetByteArray(Height)[1];
            daten[4] = Main.GetByteArray(Height)[0];
            daten[5] = erstellt[0];
            daten[6] = erstellt[1];
            daten[7] = erstellt[2];
            daten[8] = erstellt[3];
            daten[13] = Main.GetByteArray(ushort.Parse(Mapname.Length.ToString()))[0];
            daten[14] = Main.GetByteArray(ushort.Parse(Mapname.Length.ToString()))[1];
            daten[15] = Main.GetByteArray(ushort.Parse(Editor.Length.ToString()))[0];
            daten[16] = Main.GetByteArray(ushort.Parse(Editor.Length.ToString()))[1];

            Byte[] Arr_mapname = Main.GetByteArray(Mapname);
            Byte[] Arr_editor = Main.GetByteArray(Editor);
            
            offset = 17;
            for (int i = 0; i < Arr_mapname.Length; i++)
            {
                daten[offset + i] = Arr_mapname[i];
            }

            offset += Mapname.Length;
            for (int i = 0; i < Arr_editor.Length; i++)
            {
                daten[offset + i] = Arr_editor[i];
            }

            offset += Editor.Length;
            for (int i = 0; i < (Width * Height * 2); i++)
            {
                daten[offset + i] = 0x00;
            }

            map.Write(daten, 0, daten.Length);    

            map.Flush();

            return map.Name;
        }


        /// <summary>
        /// Gibt für übergebenen integer ein Byte Array mit dem entsprechenden Wert zurück
        /// </summary>
        /// <param name="Wert"></param>
        /// <returns></returns>
        public static byte[] GetByteArray(ushort Wert)
        {            
            byte[] bytes = new byte[2];
            BinaryWriter writer = new BinaryWriter(new MemoryStream(bytes));            
            writer.Write(Wert);
            writer.Close();
            return bytes;
        }

        /// <summary>
        /// Gibt für übergebenen integer ein Byte Array mit dem entsprechenden Wert zurück
        /// </summary>
        /// <param name="Wert"></param>
        /// <returns></returns>
        public static byte[] GetByteArray(int Wert)
        {
            byte[] bytes = new byte[4];
            BinaryWriter writer = new BinaryWriter(new MemoryStream(bytes));
            writer.Write(Wert);
            writer.Close();
            return bytes;
        }

        /// <summary>
        /// Gibt für übergebenen integer ein Byte Array mit dem entsprechenden Wert zurück
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] GetByteArray(string str)
        {
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            return enc.GetBytes(str);
        }

        private void pnlGrid_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = pnlGrid.CreateGraphics();
            g.FillRectangle(Brushes.Black, new Rectangle(0,0,400,1200));

            
        }

    }
}
